package com.dsunsoft.rtu.netty;

import com.dsunsoft.rtu.entity.HeartbeatInfo;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 心跳上下文对象
 *
 * @author ygm
 */
public class NettyHeartbeatMap {
    private static final Map<Integer, HeartbeatInfo> CACHE_MAP = new ConcurrentHashMap<>();

    /***
     * 添加
     * @param clientId clientId
     * @param lastReportDate lastReportDate
     */
    public static void add(Integer clientId, Date lastReportDate) {
        HeartbeatInfo heartbeat = new HeartbeatInfo(clientId,lastReportDate);
        CACHE_MAP.put(clientId, heartbeat);
    }

    /**
     * 获取
     *
     * @param clientId clazz
     * @return value
     */
    public static HeartbeatInfo get(Integer clientId) {
        if (!CACHE_MAP.containsKey(clientId)) {
            return null;
        }
        return CACHE_MAP.get(clientId);
    }

    /**
     * 获取所有
     */
    public static Map<Integer, HeartbeatInfo> get() {
        return CACHE_MAP;
    }

    /**
     * 移除
     *
     * @param clientId clientId
     */
    public static void remove(Integer clientId) {
        CACHE_MAP.remove(clientId);
    }
}
